/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.deploy.service.impl;

import com.alien.admin.modules.deploy.domain.DpResourceStandards;
import com.alien.admin.modules.system.domain.User;
import com.alien.admin.modules.system.repository.UserRepository;
import com.alien.admin.utils.*;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.deploy.repository.DpResourceStandardsRepository;
import com.alien.admin.modules.deploy.service.DpResourceStandardsService;
import com.alien.admin.modules.deploy.service.dto.DpResourceStandardsDto;
import com.alien.admin.modules.deploy.service.dto.DpResourceStandardsQueryCriteria;
import com.alien.admin.modules.deploy.service.mapstruct.DpResourceStandardsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zengzhifang
* @date 2021-09-15
**/
@Service
@RequiredArgsConstructor
public class DpResourceStandardsServiceImpl implements DpResourceStandardsService {

    private final DpResourceStandardsRepository dpResourceStandardsRepository;
    private final DpResourceStandardsMapper dpResourceStandardsMapper;
    private final UserRepository userRepository;

    @Override
    public Map<String,Object> queryAll(DpResourceStandardsQueryCriteria criteria, Pageable pageable){
        Page<DpResourceStandards> page = dpResourceStandardsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dpResourceStandardsMapper::toDto));
    }

    @Override
    public List<DpResourceStandardsDto> queryAll(DpResourceStandardsQueryCriteria criteria){
        return dpResourceStandardsMapper.toDto(dpResourceStandardsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DpResourceStandardsDto findById(Integer id) {
        DpResourceStandards dpResourceStandards = dpResourceStandardsRepository.findById(id).orElseGet(DpResourceStandards::new);
        ValidationUtil.isNull(dpResourceStandards.getId(),"DpResourceStandards","id",id);
        return dpResourceStandardsMapper.toDto(dpResourceStandards);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DpResourceStandardsDto create(DpResourceStandards resources) {

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        User user = userRepository.findByUsername(SecurityUtils.getCurrentUsername());
        resources.setCreateBy(user.getUsername());
        resources.setUpdateBy(user.getUsername());
        resources.setCreateTime(timestamp);
        resources.setUpdateTime(timestamp);

        return dpResourceStandardsMapper.toDto(dpResourceStandardsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DpResourceStandards resources) {
        DpResourceStandards dpResourceStandards = dpResourceStandardsRepository.findById(resources.getId()).orElseGet(DpResourceStandards::new);
        ValidationUtil.isNull( dpResourceStandards.getId(),"DpResourceStandards","id",resources.getId());

        User user = userRepository.findByUsername(SecurityUtils.getCurrentUsername());
        resources.setUpdateBy(user.getUsername());
        dpResourceStandards.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        dpResourceStandards.copy(resources);
        dpResourceStandardsRepository.save(dpResourceStandards);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dpResourceStandardsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DpResourceStandardsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DpResourceStandardsDto dpResourceStandards : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("规格名称", dpResourceStandards.getName());
            map.put("备注说明", dpResourceStandards.getDescript());
            map.put("创建人", dpResourceStandards.getCreateBy());
            map.put("修改人", dpResourceStandards.getUpdateBy());
            map.put("创建日期", dpResourceStandards.getCreateTime());
            map.put("更新时间", dpResourceStandards.getUpdateTime());
            map.put("cpu下限", dpResourceStandards.getMinCpu());
            map.put("内存下限", dpResourceStandards.getMinMemory());
            map.put("cpu上限", dpResourceStandards.getMaxCpu());
            map.put("内存上限", dpResourceStandards.getMaxMemory());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}